clear all
set more off
tempfile ubigeo temp

/* 

This dofile compiles all data files into the one with 94,786 observations used in the analysis.

It defines exposure variables by age groups: preschool, primary, secondary and tertiary
The overall measure if named "todo"

*/


** PATHS
local working ../working

** DATA
use `working'/300, clear
merge 1:1 conglome vivienda hogar codperso year using `working'/200
keep if _merge == 3
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/getsample
keep if _merge == 3
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/trust
drop if _merge == 2
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/identity
drop if _merge == 2
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/participation
drop if _merge == 2
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/democracy
drop if _merge == 2
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/voting
drop if _merge == 2
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/attachviolenceAT
drop if _merge == 2
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/attachviolenceA
drop if _merge == 2
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/attachviolenceT
drop if _merge == 2
drop _merge


** redefine events a dummy of occurrence
recode events* (1/999 = 1)

** create province ID
gen prov1993 = int(ubigeo1993/100)
egen provid = group(prov1993)
*tab provid, gen(provid_)

egen trend = group(year)
replace trend = trend

** recode missings in participation
recode part* (. = 0)

** create variables of exposure to conflict at different age groups
foreach t in AT A T { 
	egen preesc`t' = rowtotal(events`t'1 events`t'2 events`t'3 events`t'4 events`t'5 events`t'6)
	egen primar`t' = rowtotal(events`t'7 events`t'8 events`t'9 events`t'10 events`t'11)
	egen second`t' = rowtotal(events`t'12 events`t'13 events`t'14 events`t'15 events`t'16 events`t'17 events`t'18)
	egen tertia`t' = rowtotal(events`t'19 events`t'20 events`t'21 events`t'22 events`t'23 events`t'24 events`t'25 events`t'26 events`t'27 events`t'28 events`t'29 events`t'30)

	gen todo`t' = preesc`t' + primar`t' + second`t' + tertia`t'
}

** drop observations with ALL trust variables missing (this is already done in getsample.do)
egen nomiss = rownonmiss(trust_*)
drop if nomiss == 0
drop nomiss

drop event*

** SAVE
compress
save `working'/mergedp, replace
